package com.suzhouhe.util.response;

import com.suzhouhe.util.enums.HttpStatus;

/**
 * 返回值
 *
 * @author suzhouhe
 * @date 2020-11-03 11:47
 **/
public class Result<T> {

    /**
     * http success code 200
     */
    private static final Integer DEFAULT_SUCCESS_HTTP = HttpStatus.SUCCESS.getValue();

    /**
     * http failed code 400
     */
    private static final Integer DEFAULT_FAILED_HTTP = HttpStatus.FAIL.getValue();

    private static final String SUCCESS_MSG = "处理成功";

    private static final String FAILED_MSG = "系统错误";

    private Integer status;

    private String msg;

    private T data;

    /**
     * 普通请求，无返回值
     *
     * @return 处理成功的结果
     */
    public Result<T> success() {
        Result<T> result = new Result<>();
        result.setStatus(DEFAULT_SUCCESS_HTTP);
        result.setMsg(SUCCESS_MSG);
        return result;
    }

    /**
     * 请求有返回参数
     *
     * @param data 返回的数据
     * @return 包装数据
     */
    public Result<T> success(T data) {
        Result<T> success = this.success();
        success.setData(data);
        return success;
    }

    /**
     * 失败
     *
     * @param msg 失败原因
     * @return 失败结果包装
     */
    public Result<T> failed(String msg) {
        Result<T> result = new Result<>();
        result.setStatus(DEFAULT_FAILED_HTTP);
        result.setMsg(msg);
        return result;
    }

    /**
     * 失败，无原因描述
     *
     * @return 返回的失败
     */
    public Result<T> failed() {
        return this.failed(FAILED_MSG);
    }


    /*    get set方法               */

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }
}
